# Maintainer: Alexey Pavlov <alexpux@gmail.com>

pkgname=openssh
pkgver=8.3p1
pkgrel=1
pkgdesc='Free version of the SSH connectivity tools'
url='http://www.openssh.org/portable.html'
license=('custom:BSD')
arch=('i686' 'x86_64')
groups=('net-utils')
depends=('libfido2' 'heimdal' 'libedit' 'libcrypt' 'openssl')
makedepends=('libfido2-devel' 'libcbor-devel' 'heimdal-devel' 'libedit-devel' 'libcrypt-devel' 'openssl-devel')
source=("https://cloudflare.cdn.openbsd.org/pub/OpenBSD/OpenSSH/portable/${pkgname}-${pkgver}.tar.gz"{,.asc}
        0001-Forward-port-MSys2-patches.patch
        0002-openssh-work-around-Cygwin-declaring-setkey.patch
        0003-openssh-skip-privilege-separation-tests.patch
        0004-Allow-scp-to-copy-files-that-start-with-a-Windows-dr.patch)
sha256sums=('f2befbe0472fe7eb75d23340eb17531cb6b3aac24075e2066b41f814e12387b2'
            'SKIP'
            '572d8aa267a493205e3e6d5b194bf1800e161c3745a07e36cdf325b8014c99b8'
            '7eca1a47c2b523945899d5b7ad0418e00e7a5a30f4d6a6d3414a7328b520fd7e'
            'e06ff5a726761d067e67e0f42ecad8d4786a8f31d3269dedf034798f3e9103ca'
            'edad721c623aa96506c23444aedfdea8b7a4659574b55b05755e913879dbf3c6')
validpgpkeys=('59C2118ED206D927E667EBE3D3E5F56B6D920D30')

backup=('etc/ssh/ssh_config' 'etc/ssh/sshd_config')

prepare() {
  cd "${srcdir}/${pkgname}-${pkgver}"
  patch -p1 -i ${srcdir}/0001-Forward-port-MSys2-patches.patch
  patch -p1 -i ${srcdir}/0002-openssh-work-around-Cygwin-declaring-setkey.patch
  patch -p1 -i ${srcdir}/0003-openssh-skip-privilege-separation-tests.patch
  patch -p1 -i ${srcdir}/0004-Allow-scp-to-copy-files-that-start-with-a-Windows-dr.patch
  autoreconf -fvi
}

build() {
  cd "${srcdir}/${pkgname}-${pkgver}"

  TEST_SSH_UTF8=no \
  ./configure \
    --build=${CHOST} \
    --prefix=/usr \
    --sbindir=/usr/bin \
    --libexecdir=/usr/lib/ssh \
    --sysconfdir=/etc/ssh \
    --localstatedir=/var \
    --with-libedit \
    --with-kerberos5=/usr \
    --disable-strip \
    --with-security-key-builtin

  make
}

check() {
  cd "${srcdir}/${pkgname}-${pkgver}"

  make tests ||
  grep $USER /etc/passwd | grep -q /bin/false
  # connect.sh fails when run with stupid login shell
}

package() {
  cd "${srcdir}/${pkgname}-${pkgver}"

  make DESTDIR="${pkgdir}" install

  #ln -sf ssh.1.gz "${pkgdir}"/usr/share/man/man1/slogin.1.gz
  install -Dm644 LICENCE "${pkgdir}/usr/share/licenses/${pkgname}/LICENCE"

  install -Dm755 contrib/findssl.sh "${pkgdir}"/usr/bin/findssl.sh
  install -Dm755 contrib/ssh-copy-id "${pkgdir}"/usr/bin/ssh-copy-id
  install -Dm644 contrib/ssh-copy-id.1 "${pkgdir}"/usr/share/man/man1/ssh-copy-id.1

  #rm -f "${pkgdir}"/etc/ssh/sshd_config
  #rm -f "${pkgdir}"/usr/bin/sshd.exe
  #rm -f "${pkgdir}"/usr/sbin/sshd.exe
  #rm -f "${pkgdir}"/usr/lib/ssh/sftp-server.exe
}
